package com.nh.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


import com.nh.mapper.TbspecMapper;
import com.nh.po.Tbspec;
import com.nh.po.TbspecExample;
import com.nh.service.TbService;

import com.nh.util.PageResult;

import com.nh.vo.TbspecVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TbServiceImpl implements TbService {
    @Autowired
    private TbspecMapper tbspecMapper;
    @Override
    public List<Tbspec> findAll() {
        List<Tbspec> tbspecs = tbspecMapper.selectAll();
        tbspecs.forEach(a ->{
            System.out.println(a.getId());
            System.out.println(a.getSpecName());
        });
        return tbspecMapper.selectAll();
    }

    @Override
    public PageResult<Tbspec> findByPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Tbspec> users = this.findAll();
        Page<Tbspec> userPage = (Page<Tbspec>) users;
        return  new PageResult<>(userPage.getResult(),userPage.getTotal());
    }

    @Override
    public PageResult<Tbspec> search(Integer page, Integer pageSize, TbspecVo tbspecVo) {
        PageHelper.startPage(page,pageSize);
        if (tbspecVo != null){
            TbspecExample tbspecExample = new TbspecExample();
            TbspecExample.Criteria criteria = tbspecExample.createCriteria();
            if(StrUtil.isNotBlank(tbspecVo.getSpec_name())){
                criteria.andSpecNameLike("%" + tbspecVo.getSpec_name() + "%");
            }
            List<Tbspec> tbspecs = tbspecMapper.selectByExample(tbspecExample);
            Page<Tbspec> tbspecPage = (Page<Tbspec>) tbspecs;

            return  new PageResult<>(tbspecPage.getResult(),tbspecPage.getTotal());
        }else{
            List<Tbspec> tbspecs = this.findAll();
            Page<Tbspec> tbspecPage = (Page<Tbspec>) tbspecs;
            return  new PageResult<>(tbspecPage.getResult(),tbspecPage.getTotal());}
    }

    @Override
    public boolean save(Tbspec tbspec) {
        return tbspecMapper.insert(tbspec) > 0;
    }
}
